//
//  PhotoBrowserViewController.swift
//  weibo117
//
//  Created by 宋禹奇 on 15/7/10.
//  Copyright © 2015年 宋禹奇. All rights reserved.
//

import UIKit
import FFAutoLayout

class PhotoBrowserViewController: UIViewController {
    override func loadView() {
        view = UIView()
        
        //添加控件
        view.addSubview(collectionView)
        view.addSubview(closeButton)
        view.addSubview(saveButton)
        
        //控件自动布局
        collectionView.ff_Fill(view)
        
        closeButton.ff_AlignInner(ff_AlignType.BottomLeft, referView: view, size: CGSize(width: 60, height: 30), offset: CGPoint(x: 12, y: -12))
        saveButton.ff_AlignInner(ff_AlignType.BottomRight, referView: view, size: CGSize(width: 60, height: 30), offset: CGPoint(x: -12, y: -12))
        
        //添加监听方法
        closeButton.addTarget(self, action: "close", forControlEvents: UIControlEvents.TouchUpInside)
        saveButton.addTarget(self, action: "save", forControlEvents: UIControlEvents.TouchUpInside)
        
        prepareCollectionView()
    }
    
    //准备collectionView
    func prepareCollectionView() {
        collectionView.dataSource = self
    
        collectionView.registerClass(PhotoViewerCell.self, forCellWithReuseIdentifier: YUPhotoBrowserCellReuseIdentifier)
    }
    
    //布局
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        prepareLayout()
        
    }
    
    //准备布局
    private func prepareLayout() {
    
        layout.itemSize = collectionView.bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        collectionView.pagingEnabled = true

    }
    
    //关闭
    func close() {
        dismissViewControllerAnimated(true, completion: nil)
    
    }
    
    //保存
    func save() {
        print(__FUNCTION__)
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    //构造函数
    init(urls: [NSURL], index: Int) {
    
        //初始化本类属性
        imageURLs = urls
        
        currentIndex = index
    
        //调用父类的构造函数
        super.init(nibName: nil, bundle: nil)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //图像URL数组
    private var imageURLs: [NSURL]
    
    //图像索引
    private var currentIndex: Int

    
    //懒加载控件
    lazy private var layout = UICollectionViewFlowLayout()
    lazy private var collectionView:UICollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: self.layout)
    lazy private var closeButton: UIButton = UIButton(title: "关闭", fontSize: 14)
    lazy private var saveButton: UIButton = UIButton(title: "保存", fontSize: 14)

}

//PhotoBrowserViewController类 方法
private let YUPhotoBrowserCellReuseIdentifier = "YUPhotoBrowserCellReuseIdentifier"

extension PhotoBrowserViewController: UICollectionViewDataSource {

    //数据源方法
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageURLs.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(YUPhotoBrowserCellReuseIdentifier, forIndexPath: indexPath) as! PhotoViewerCell
        
        cell.backgroundColor = UIColor.randomColor()

        cell.imageURL = imageURLs[indexPath.item]
        
        print(imageURLs)
        print("--------------")
        print(cell.imageURL)
        
        return cell
        
    }
    

}



